package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

// 从1加到100
func sum1to100() {
	sum := 0
	for i := 1; i <= 100; i++ {
		sum += i
	}
	fmt.Println(sum)
}

// 整数转二进制
/**
除2取余数（对应的程序就是取2的模），并反序列
13/2 -->1
6/2   -->0
3/2   -->1
1/2   -->1
十进制13 = 二进制1101
*/
func convertToBin(n int) string {
	result := ""
	for ; n > 0; n /= 2 {
		lsb := n % 2 //取模运算
		result = strconv.Itoa(lsb) + result
	}
	return result
}

func printFile(filename string) {
	file, err := os.Open(filename)
	if err != nil {
		panic(err)
	}

	printFileContents(file)
}

func printFileContents(reader io.Reader) {
	scanner := bufio.NewScanner(reader)

	for scanner.Scan() {
		fmt.Println(scanner.Text())
	}
}

func forever() {
	for {
		fmt.Println("abc")
	}
}

func main() {
	//sum1to100()

	fmt.Println("convertToBin results:")
	fmt.Println(
		convertToBin(5),  // 101
		convertToBin(13), // 1101
		convertToBin(72387885),
		convertToBin(0),
	)

	/*

		fmt.Println("abc.txt contents:")
		printFile("basic/branch/abc.txt")

		fmt.Println("printing a string:")
		s := `abc"d"
		kkkk
		123

		p`
		printFileContents(strings.NewReader(s))

		// Uncomment to see it runs forever
		// forever()
	*/

}
